<template>
  <div class="content">
    <div>
      <MySearch :form-inline="formInline" @searchFn="searchFn">
        <template>
          <el-col :span="8">
            <el-form-item label="库区名称" prop="areaName">
              <el-input v-model="formInline.areaName" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库位名称" prop="name">
              <el-input v-model="formInline.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="仓库状态" prop="status">
              <el-select v-model="formInline.status" placeholder="请选择" clearable>
                <el-option label="全部" :value="null" />
                <el-option label="停用" value="0" />
                <el-option label="启用" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </template>
      </MySearch>
    </div>
    <div class="table">
      <div>
        <MyTable :table-data="locationList" :table-col="tableCol" :current="current" :size="size">
          <template #btn>
            <div style="display:flex;justify-content: space-between;">
              <el-button class="add" @click="$router.push({ name: 'location-details', params: { id: 'null' } })">新增仓库
              </el-button>
              <div>
                <el-button type="" class="unimportanceButton" @click="downloadTemplate">下载库位模板</el-button>
                <el-button type="" class="unimportanceButton" @click="uploadTemplate">导入库位信息</el-button>
              </div>
            </div>
          </template>
          <template #edit="props">
            <el-button
              type="text"
              size="small"
              @click="$router.push({ name: 'location-details', params: { id: props.status.id } })"
            >编辑</el-button>
            <el-button type="text" size="small" @click="editStatus(props.status)">{{ status(props.status.status) }}
            </el-button>
            <el-button type="text" size="small" @click="delFn(props.status)">删除</el-button>
          </template>
        </MyTable>
      </div>
      <div>
        <MyPage :total-page="totalPage" @changeSize="changeSize" @changeCurrent="changeCurrent" />
      </div>
    </div>
    <MyDialog :dialog-visible="dialogVisible" @closeFn="dialogVisible=false" />
  </div>
</template>

<script>
import MySearch from '@/components/MySearch'
import MyTable from '@/components/MyTable'
import MyPage from '@/components/MyPage'
import MyDialog from './children/myUpload.vue'
import { locationPage, editLocation, delLocation } from '@/api'
export default {
  components: { MySearch, MyTable, MyPage, MyDialog },
  data() {
    return {
      formInline: {},
      locationList: [],
      tableCol: [
        { prop: 'warehouseName', label: '所属仓库', width: '150px' },
        { prop: 'areaCode', label: '库区编码', width: '150px' },
        { prop: 'areaName', label: '库区名称', width: '150px' },
        { prop: 'code', label: '库位编号', width: '200px' },
        { prop: 'name', label: '库位名称', width: '150px' },
        { prop: 'temperatureType', label: '温度类型', width: '150px' },
        { prop: 'bearingType', label: '承重类型', width: '150px' },
        { prop: 'useType', label: '用途属性', width: '150px' },
        { prop: 'textstatus', label: '停用状态', width: '150px' },
        { prop: 'maxNum', label: '承载上限', width: '150px' },
        { prop: 'updateTime', label: '更新时间', width: '200px' }
      ],
      totalPage: null,
      size: 10,
      current: 1,
      dialogVisible: false
    }
  },
  async created() {
    await this.warehouseFn(this.formInline, this.size, this.current)
  },
  methods: {
    // 请求
    async warehouseFn(obj, size, current) {
      const res = await locationPage({
        ...obj,
        current,
        size,
        descs: 'createTime'
      })
      this.totalPage = Number(res.data.data.total)
      this.locationList = res.data.data.records
      // 数据处理
      this.locationList.forEach(item => {
        if (item.bearingType === 'ZX') {
          item.bearingType = '重型'
        } else if (item.bearingType === 'QX') {
          item.bearingType = '轻型'
        } else {
          item.bearingType = '中型'
        }
        if (item.temperatureType === 'CW') {
          item.temperatureType = '常温'
        } else if (item.temperatureType === 'LC') {
          item.temperatureType = '冷藏'
        } else {
          item.temperatureType = '恒温'
        }
        if (item.useType === 'RKHCQ ') {
          item.useType = '入库缓存区'
        } else if (item.useType === 'CKHCQ') {
          item.useType = '出库缓存区'
        } else if (item.useType === 'CCQ ') {
          item.useType = '存储区'
        } else if (item.useType === 'FJQ ') {
          item.useType = '分拣区'
        } else {
          item.useType = '质检区'
        }
        if (item.status === 0) {
          item.textstatus = '停用'
        } else {
          item.textstatus = '启用'
        }
      })
      return res.data
    },
    // 搜索
    async searchFn() {
      const res = await this.warehouseFn(this.formInline, this.size, this.current)
      console.log(res)
    },
    // 改变页数
    async changeSize(val) {
      this.size = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    async changeCurrent(val) {
      this.current = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    // 修改状态
    editStatus(data) {
      this.$confirm(`确定要${this.status(data.status)}：${data.name} 吗？`, `确认${this.status(data.status)}`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await editLocation({ id: data.id, status: data.status === 1 ? 0 : 1 })
        await this.warehouseFn(this.formInline, this.size, this.current)
        this.$message({
          type: 'success',
          message: `${this.status(data.status)}成功!`
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消${this.status(data.status)}`
        })
      })
    },
    async delFn(data) {
      this.$confirm(`确定要删除吗？`, `提示`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delLocation(data.id)
        await this.warehouseFn(this.formInline, this.size, this.current)
        this.$message({
          type: 'success',
          message: `删除成功!`
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消删除`
        })
      })
    },
    // 状态显示
    status(status) {
      return status === 1 ? '停用' : '启用'
    },
    // 点击模板按钮触发a下载文件
    downloadTemplate() {
      const link = document.createElement('a')
      link.setAttribute('download', '')
      link.href = '/template/locationTemplate.xlsx' // 你本地资源文件的存放地址
      console.log('href:', link.href)
      link.click()
    },
    // 上传弹窗
    uploadTemplate() {
      this.dialogVisible = true
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';
@import '@/styles/common.scss';

</style>
